<template>
  <div class="app-container">
    <!--工具栏-->
    <crudOperation :permission="permission"/>
    <div class="head-container">
      <div v-if="!crud.props.searchToggle">
      </div>
      <el-dialog v-dialog-drag style="width: 80vm" title="搜索"   close-on-click-modal :visible.sync="crud.props.searchToggle"
        width="70%">
        <el-form size="small" class="formClass" label-width="85px" >
          <el-row :gutter="10">
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="查询类型">
                <el-select v-model="query.queryType" collapse-tags @change="queryTypeChange" class="edit-input" placeholder="请选择查询类型" style="width: 100%;">
                  <el-option label="汇总" value="1"/>
                  <el-option label="明细" value="2"/>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" >
              <el-form-item label="业务日期">
                <div class="flex flex-row">
                  <el-date-picker
                    v-model="query.startDate"
                    align="right"
                    unlink-panels
                    class="fl1"
                    format="yyyy - MM - dd "
                    value-format="yyyy-MM-dd"
                    default-value
                    range-separator=""
                    placeholder="开始日期"
                  />
                  <el-date-picker
                    v-model="query.endDate"
                    align="right"
                    unlink-panels
                    class="fl1"
                    format="yyyy - MM - dd "
                    value-format="yyyy-MM-dd"
                    default-value
                    range-separator=""
                    placeholder="结束日期"
                  />
                </div>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="借款项目">
                <el-select v-model="query.projectType" collapse-tags class="edit-input" placeholder="借款项目" style="width: 100%;">
                  <el-option label="内部借款" value="1"/>
<!--                  <el-option label="内部借款" value="2"/>-->
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
              <el-form-item label="借款人">
                <zSelect :value.sync="query.userIds" multiple url="/api/dropDown/sysUserList" style="width:  100%;"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row class="flex fow-reverse" >
            <el-button class="filter-item"  size="mini" type="primary" @click="queryReport">搜索</el-button>
            <el-button class="filter-item" plain   size="mini" type="primary"
                       @click="resetQuery" >重置</el-button>
          </el-row>
        </el-form>
      </el-dialog>

      <!--表格渲染-->
      <el-table ref="table" border :data="crud.data" size="small"
                style="width: 100%;"
                @header-dragend="changeColWidth"
                show-summary
                height="700"
                :summary-method="sumRow"
                @row-click="crud.selectRow"
                :row-style="crud.rowClass"
                :row-class-name="crud.tableRowClassName"
                @contextmenu.native="showMenu"
                @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" width="55" label="序号"/>
        <el-table-column :render-header="crud.renderHeader" v-if="query.queryType === '2'" prop="busDate" label="业务日期"/>
        <el-table-column v-if="columns.visible('userCode')" prop="userCode" label="借款人编码" />
        <el-table-column v-if="columns.visible('userName')" prop="userName" label="借款人名称" />
        <el-table-column v-if="columns.visible('projectType')" prop="projectType" label="借款项目" />
        <el-table-column v-if="columns.visible('loanAmount')" prop="loanAmount" label="借款金额" />
        <el-table-column v-if="columns.visible('repayAmount')" prop="repayAmount" label="还款金额" />
        <el-table-column v-if="columns.visible('unRepayAmount')" prop="unRepayAmount" label="借款余额" />
        <el-table-column :render-header="crud.renderHeader" v-if="query.queryType === '2'" prop="orderCode" label="单据号" width="120">
          <template slot-scope="scope">
            <span v-for="(item,index) in getIdCodeList(scope.row,1)"
                  style="color:#409eff;cursor: pointer;text-decoration: #409eff"
                  @click="jumpDetail(item.id,1)">{{ item.code }}
            </span>
          </template>
        </el-table-column>
        <el-table-column :render-header="crud.renderHeader" v-if="query.queryType === '2'" prop="bankName" label="账户名称"/>
        <el-table-column :render-header="crud.renderHeader" v-if="query.queryType === '2'" prop="orderStatus" label="单据状态"/>
        <el-table-column :render-header="crud.renderHeader" v-if="query.queryType === '2'" prop="deptName" label="部门"/>
        <el-table-column :render-header="crud.renderHeader" v-if="query.queryType === '2'" prop="createUserName" label="制单人"/>
        <el-table-column :render-header="crud.renderHeader" v-if="query.queryType === '2'" prop="remark" label="备注"/>
      </el-table>
      <!--分页组件-->

      <pagination/>
    </div>
  </div>
</template>

<script>
import crudReportBankInOutLoan from '@/api/erp/reportBankInOutLoan'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import Treeselect from '@riophae/vue-treeselect'
import {get} from '@/api/erp/reportBankInOutLoan'
import { publicListKey } from "@/utils/key";
import {tablemixins} from "@/mixins/tablemixins";
import  jTableColumnConfig  from "@/components/jTableColumnConfig/jTableColumnConfig";
import tableSelect from '@/components/tableSelect/tableSelect'
import { basSubjectDropDownTree } from '@/api/erp/basSubject'


// crud交由presenter持有
const defaultCrud = CRUD({ title: '银行收支借款单报表',
  optShow: {
    add: false,
    edit: false,
    del: false,
    download: true,
    leadingIn: false,
    enable: false,
    disable: false,
    pass: false,
    reject: false,
    invalid: false,
    recover: false,
    downloadDetail: false,
    print: false,
    printDetail: false
  },
  query:{
    queryType:1,
    projectType:1,
    startDate:new Date().toISOString().split('T')[0],
    endDate:new Date().toISOString().split('T')[0]
  },
  queryOnPresenterCreated:false,
  url: 'api/reportBankInOutLoan', sort: '',
  crudMethod: { ...crudReportBankInOutLoan }})
const defaultForm = { queryType: null, startDate: null, endDate:null}
export default {
  name: 'ReportBankInOutLoan',
  components: { pagination, crudOperation, rrOperation, udOperation,Treeselect,jTableColumnConfig, tableSelect },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud(),publicListKey,tablemixins],
  dicts:[],
  mounted(){
    this.crud.props.searchToggle = false
  },
  created() {
    this.query.queryType = '1'
    this.query.projectType = '1'
    this.$set(this.query,'startDate',new Date().toISOString().split('T')[0])
    this.$set(this.query,'endDate',new Date().toISOString().split('T')[0])
    basSubjectDropDownTree().then(res=>{
      this.subjectList = res
    })
  },
  data() {
    return {
      subjectIds:[],
      subjectList: [],
      queryType:'',
      permission: {
        export: ['admin', 'reportBankInOutLoan:export']
      },
      rules: {
        queryType: [
          { required: true, message: '查询类型不能为空', trigger: 'blur' }
        ]
      },
    }
  },
  updated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  watch: {
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },
    sumRow({ columns, data }){
      const sums = []
      for (let i in  columns) {
        if (['loanAmount','repayAmount','unRepayAmount'].includes(columns[i].property)) {
          let selectSum = 0
          this.$refs.table.selection.forEach(item => {
            selectSum += item[columns[i].property]
          })
          sums[i] = <p>{selectSum}
            <hr/>
            {this.crud.summary[columns[i].property] || 0}</p>
        }
        sums[0] = <p>选中
          <hr/>
          合计</p>
      }
      return sums
    },
    init(){
      this.page = 1
    },
    onGet(){
      const that = this
      let { page,size } = this.crud.page
      page =  1
      get({...this.query,page,size}).then(res=>{
        that.crud.data = res.data.content
        that.crud.page.total = res.data.totalElements
        console.log(that.total)
      })
    },
    resetQuery(){
      this.crud.resetQuery()
      this.query.queryType = '1'
      this.query.projectType = '1'
      this.query.userIds = ''
      this.$set(this.query,'startDate',new Date().toISOString().split('T')[0])
      this.$set(this.query,'endDate',new Date().toISOString().split('T')[0])
      this.queryTypeChange()
    },
    queryReport(){
      this.init()
      this.crud.toQuery()
      this.crud.props.searchToggle = false
    },
    getIdCodeList(row,type){
      if(row.orderId && type === 1){
        const idList = row.orderId.split(",");
        const codeList = row.orderCode.split(",");
        const lst = []
        idList.forEach(function (id, index) {
          lst.push({"id":id,"code":codeList[index]})
        })
        return lst;
      }
    },
    jumpDetail(id,type){
      let path = '/finance/financeBankInOutBorrowInDetail/'
      this.$router.push({path:`${path}${id}`})
    },
  }
}
</script>

<style lang="scss" scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
  .flex{
    display: flex;
  }
  .fow-reverse{
    flex-direction: row-reverse;
  }
  .codeJump{
    background-image:-webkit-linear-gradient(top,yellow,#ff00fb);
    -webkit-background-clip:text;
    -webkit-text-fill-color:transparent;
    cursor: pointer;
  }
</style>
